/*
 * author      : 吴中勤 
 * date        : 2018/8/16
 * description : 从 moby的数据中, 创建一个 SAP BP请求需要的信息
 */

package com.deepexi.adapter.converter.sap.bp;

import com.deepexi.adapter.converter.sap.sapdict.SapDictService;
import com.deepexi.adapter.converter.sap.sapdict.SapDictType;
import com.deepexi.adapter.facade.moby.request.MobyEventBody;
import com.deepexi.adapter.facade.moby.request.MobyMember;
import com.deepexi.adapter.sdk.sap.SapUtils;
import com.deepexi.adapter.sdk.sap.dto.bp.create.BPCreateInputVO;
import com.deepexi.adapter.sdk.sap.dto.bp.create.SapBPCreateRequestBody;
import com.deepexi.adapter.sdk.sap.dto.bp.extend.BPExtendInputVO;
import com.deepexi.adapter.sdk.sap.dto.bp.extend.SapBPExtendRequestBody;
import com.deepexi.adapter.sdk.sap.exception.SapBizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BPRequestAssembler {

    @Autowired
    private SapDictService dictService;

    private static final Logger logger = LoggerFactory.getLogger(BPRequestAssembler.class);

    /** 组装请求体: 创建BP */
    public SapBPCreateRequestBody assembleCreateBP(MobyEventBody body) throws SapBizException {
        SapBPCreateRequestBody bpBody = new SapBPCreateRequestBody();
        SapUtils.fillUuidAndSecret(bpBody);

        bpBody.setShopCode(body.getMobyTradeOrder().getShopCode());
        bpBody.setPartner(body.getMobyTradeOrder().getScCode());
        bpBody.addBPItem(getBPInfoFromRequest(body));

        return bpBody;
    }

    /** 从 EffectiveOrderRequestBody中抽取信息, 合成 BPCreateInputVO */
    private BPCreateInputVO getBPInfoFromRequest(MobyEventBody body) throws SapBizException {
        MobyMember member = body.getMobyMember();
        BPCreateInputVO bpItem = new BPCreateInputVO();

        bpItem.setMobileNo(member.getPhone());
        bpItem.setName(member.getName());
        bpItem.setIdType(mapCredentialType(member.getIdType()));
        bpItem.setIdNo(member.getIdNum());
        bpItem.setTitle(genderToTitle(member.getGender()));
        bpItem.setAddress(member.getAddr());

        // choice
        return bpItem;
    }

    /** 性别 -> 称谓 */
    private String genderToTitle(String mobyGender) throws SapBizException {
        String sapValue = dictService.getSapValueByMoby(SapDictType.GENDER, mobyGender);
        if (null == sapValue)
            throw new SapBizException("性别找不到对应, moby值=" + mobyGender);
        return sapValue;
    }

    private String mapCredentialType(String mobyIdType) throws SapBizException {
        String sapValue = dictService.getSapValueByMoby(SapDictType.CREDENTIAL, mobyIdType);
        if (null == sapValue)
            throw new SapBizException("性别找不到证件类型, moby值=" + mobyIdType);
        return sapValue;
    }

    /** 组装请求体: 创建BP */
    private BPExtendInputVO getBPExtendInfo(MobyEventBody body, SapAdapterMember extendsFrom) {
        String memberId = body.getMobyMember().getId();
        if (!memberId.equals(extendsFrom.getMemberId())) {
            throw new RuntimeException("扩展BP, 但memberId 不匹配");
        }

        BPExtendInputVO bpItem = new BPExtendInputVO();
        bpItem.setBpNum(extendsFrom.getBpNum());

        return bpItem;
    }

    /** 组装请求体: 扩展BP */
    public SapBPExtendRequestBody assembleExtendBP(MobyEventBody body, SapAdapterMember extendsFrom) {
        SapBPExtendRequestBody bpBody = new SapBPExtendRequestBody();
        SapUtils.fillUuidAndSecret(bpBody);
        bpBody.setShopCode(body.getMobyTradeOrder().getShopCode());
        bpBody.setPartner(body.getMobyTradeOrder().getScCode());

        bpBody.addBPItem(getBPExtendInfo(body, extendsFrom));
        return bpBody;
    }

}
